package com.hotel.dao.impl;

import com.hotel.dao.RoomDao;
import com.hotel.entity.Room;
import com.hotel.exception.EntityException;
import com.hotel.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author Str
 * @Date 2021/10/29 22:11
 * @Description :
 */
public class RoomDaoImpl implements RoomDao {
    @Override
    public List<Room> getRooms() throws SQLException {
        String sqlQuery = "select * from room";
        List<Room> rooms = JdbcUtils.queryList(sqlQuery, Room.class);
        return rooms;
    }

    @Override
    public Room getRoomById(String id) {
        String sql = "select n.*, r.typeName,r.dayPrice,s.floor from room n " +
                " left join roomtype r on r.id=n.typeId" +
                " left join storey s on s.id=n.storeyId" +
                " where n.id = ?";
        return JdbcUtils.queryObject(sql, Room.class, id);
    }

    @Override
    public Room getRoomByRoomNo(String roomNo) throws EntityException {
        //{1}获取一个普通用户 (不含管理员)
        String sql = "select n.*, r.typeName,r.dayPrice,s.floor from room n " +
                " left join roomtype r on r.id=n.typeId" +
                " left join storey s on s.id=n.storeyId" +
                " where n.roomNo = ?";
        Room room = JdbcUtils.queryObject(sql, Room.class, roomNo);
        //{1}如果我是一定要得到这个 user, 但拿不到, 报出异常。
//        if (room == null) {
//            throw new EntityException("此房间不存在");
//        }
        //{2}这里由于使用了 QueryRunner 无法关闭 rs, pmst.
        return room;
    }

    /**
     * 获取搜索语句片段
     * map是url参数列表的map集合
     *
     * @return java.lang.String
     */
    @Override
    public String getSearchSQL(Map<String, Object> map) {
        //{ps}这个 map 进来这个方法之前先把 page, limit 拿掉。
        Set<String> keySet = map.keySet();
        StringBuilder sb = new StringBuilder();
        for (String key : keySet) {
            Object word = map.get(key);
            sb.append(" and " + key
                    + " like '%" + word + "%'");
        }
        return sb.toString();
    }

    /**
     * sqlPart是getSearchSQL方法的返回值
     *
     * @return int
     */
    @Override
    public int getCount(String sqlPart) throws SQLException {
        //{1}做 sql 拼接。
        String sql = "select count(*) from room n " +
                " left join roomtype r on r.id=n.typeId" +
                " left join storey s on s.id=n.storeyId" +
                " where n.id is not null " + sqlPart;
        System.out.println("getCount" + sql);
        //{2}查询数量。
        Long count = (Long) JdbcUtils.queryValue(sql);
        //{3}做类型转换 ----> int
        return count.intValue();
    }

    @Override
    public List<Room> getList(String sqlPart, Integer offset, Integer limit) throws SQLException {
        String sql = "select n.*, r.typeName,r.dayPrice,s.floor from room n " +
                " left join roomtype r on r.id=n.typeId" +
                " left join storey s on s.id=n.storeyId" +
                " where n.id is not null " + sqlPart + " order by n.id limit ?, ?";
        List<Room> list = JdbcUtils.queryList(sql, Room.class, offset, limit);
        System.out.println("getRoomList" + list);
        return list;
    }

    @Override
    public int addRoom(Room room) throws SQLException {
        // 新增房间默认空置中
        String sqlAdd = "INSERT INTO `room` VALUES (?, ?, ?, ?, '空置中', now(), now());";
        return JdbcUtils.update(sqlAdd, room.getId(), room.getRoomNo(), room.getTypeId(), room.getStoreyId());
    }

    @Override
    public int updateRoom(Room room) throws SQLException {
        String sqlUpdate = "update room set roomNo=?,typeId=?,storeyId=?,status=? where id=?;";
        return JdbcUtils.update(sqlUpdate, room.getRoomNo(), room.getTypeId(), room.getStoreyId(), room.getStatus(), room.getId());
    }

    @Override
    public int deleteRoom(String id) throws SQLException {
        String sqlDelete = "delete from room where id=?";
        return JdbcUtils.update(sqlDelete, id);
    }
}
